package com.itheima.jdbcdemo;

import org.junit.jupiter.api.Test;

import java.sql.*;

/**
 * 回顾jdbcapi
 */
public class JdbcDemo1 {
    @Test
    public void test1() throws ClassNotFoundException, SQLException {
        //注册驱动
       // Class.forName("com.mysql.jdbc.Driver");
        //获取连接对象
        String url = "jdbc:mysql://192.168.203.128:3306/case2test";
        String userName="root";
        String password="";
        Connection connection = DriverManager.getConnection(url,userName,password);
        //创建查询的sql语句
        String sql ="select * from account where id=?";
        PreparedStatement statement = connection.prepareStatement(sql);
        //注入参数执行查询
        statement.setInt(1,2);

        //拿到结果集对象
        ResultSet result = statement.executeQuery();
        //循环获取查询的数据
        while(result.next()){
            int id = result.getInt("id");

            String name = result.getString("name");

            float money = result.getFloat("money");

            //打印结果
            System.out.println(id+"  "+name+"  "+money);
        }

        //释放资源
        result.close();
        statement.close();
        connection.close();
    }
}
